#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include "builderplugin.h"

dvb_funcs_t stDvbFuncsList = {0};

//dvb export functions
void PluginDvbLogcat(char *ps8Format, ...)
{
	int s32Ret = -1;
	
	if(stDvbFuncsList.m_pfDvbLogcat)
	{
		char		as8Buffer[4096];
		va_list	stVa;
		int s32Len;

		va_start(stVa, ps8Format);
		s32Len = vsnprintf(as8Buffer, sizeof(as8Buffer) - 1, ps8Format, stVa);
		va_end(stVa);
		if (-1 == s32Len)
		{
			s32Len = sizeof(as8Buffer) -1;
		}
		as8Buffer[s32Len] = '\0';
		stDvbFuncsList.m_pfDvbLogcat(as8Buffer);
	}
	
	return;	
}

int PluginDvbSetFilter(unsigned int* phFilter, unsigned short u16Pid, unsigned char* pu8Data, unsigned char* pu8Mask, unsigned int u32Depth)
{
	int s32Ret = -1;
	if(stDvbFuncsList.m_pfDvbSetFilter)
	{
		s32Ret = stDvbFuncsList.m_pfDvbSetFilter(phFilter, u16Pid, pu8Data, pu8Mask, u32Depth);
	}
	return s32Ret;
}

int PluginDvbReleaseFilter(unsigned int hFilter)
{
	int s32Ret = -1;
	if(stDvbFuncsList.m_pfDvbReleaserFilter)
	{
		s32Ret = stDvbFuncsList.m_pfDvbReleaserFilter(hFilter);
	}
	return s32Ret;
}

int PluginDvbTunerLock(builder_tuner_t* pstFreq)
{
	int s32Ret = -1;
	PluginDvbLogcat("PluginDvbTunerLock() stDvbFuncsList.m_pfDvbTunerLock= %d\r\n", stDvbFuncsList.m_pfDvbTunerLock);
	
	if(stDvbFuncsList.m_pfDvbTunerLock)
	{
		s32Ret = stDvbFuncsList.m_pfDvbTunerLock(pstFreq);
	}
	return s32Ret;
}

int PluginDvbWriteNvm(unsigned char* pu8Data, unsigned int u32Len, unsigned int u32Offset)
{
	int s32Ret = -1;
	if(stDvbFuncsList.m_pfDvbWriteNvm)
	{
		s32Ret = stDvbFuncsList.m_pfDvbWriteNvm(pu8Data, u32Len, u32Offset);
	}
	return s32Ret;
}

int PluginDvbReadNvm(unsigned char* pu8Data, unsigned int u32Len, unsigned int u32Offset)
{
	int s32Ret = -1;
	if(stDvbFuncsList.m_pfDvbReadNvm)
	{
		s32Ret = stDvbFuncsList.m_pfDvbReadNvm(pu8Data, u32Len, u32Offset);
	}
	return s32Ret;	
}

void* PluginDvbMemAlloc(unsigned int pu32Size)
{
	if(stDvbFuncsList.m_pfDvbMemAlloc)
	{
		return stDvbFuncsList.m_pfDvbMemAlloc(pu32Size);
	}
	return NULL;
}

void PluginDvbMemFree(void *pPointer)
{
	if(stDvbFuncsList.m_pfDvbMemFree)
	{
		stDvbFuncsList.m_pfDvbMemFree(pPointer);
	}	
}

void PluginDvbConvertString(unsigned char *pString, unsigned short len, unsigned char *lanCode)
{
	if(stDvbFuncsList.m_pfDvbConvertString)
	{
		stDvbFuncsList.m_pfDvbConvertString(pString, len, lanCode);
	}	
}

int PluginDvbTableChangleNotify(unsigned char u8TableChangeResult)
{
	int s32Ret = -1;

	if(stDvbFuncsList.m_pfDvbTableChangeNotify)
	{
		s32Ret = stDvbFuncsList.m_pfDvbTableChangeNotify(u8TableChangeResult);
	}

	return s32Ret;
}

int PluginDvbReadMonitorData(builder_monitor_data_t *pstmonitorData)
{
	int s32Ret = -1;
	
	if(stDvbFuncsList.m_pfDvbTableChangeNotify)
	{
		s32Ret = stDvbFuncsList.m_pfDvbReadMonitorData(pstmonitorData);
	}
	
	return s32Ret;
}

void PluginDvbSetFun(dvb_funcs_t* pstDvbFuncs)
{
	stDvbFuncsList = *pstDvbFuncs;

}


//common fun
int PluginCheckTunerInfo(builder_tuner_t* pstFreq)
{
	int s32Ret = -1;

	PluginDvbLogcat("PluginCheckTunerInfo() - entry, pstFreq = %p\r\n", pstFreq);	
	if(pstFreq)
	{
		PluginDvbLogcat("PluginCheckTunerInfo() - entry, FreqInfo = [%d, %d, %d, %d, %d]\r\n", pstFreq->m_u32Freq, pstFreq->m_u32Mod, pstFreq->m_u32Bandwidth, pstFreq->m_u32Sym, pstFreq->m_u32Tunertype);
		if(pstFreq->m_u32Freq < 42000 || pstFreq->m_u32Freq > 862000 || pstFreq->m_u32Bandwidth != 8 
			|| (pstFreq->m_u32Mod != 16 && pstFreq->m_u32Mod != 32 && pstFreq->m_u32Mod != 64 && pstFreq->m_u32Mod != 128 && pstFreq->m_u32Mod != 256))
		{
			PluginDvbLogcat("PluginCheckTunerInfo() - check error, pstFreq->m_u32Freq = %d\r\n", pstFreq->m_u32Freq );
			s32Ret = -1;
		}
		else
		{
			s32Ret = 0;
		}
	}
	PluginDvbLogcat("PluginCheckTunerInfo() - return, s32Ret = %d\r\n", s32Ret);
	return s32Ret;
}

int PluginCreatSectionNode(builder_section_t** ppSectionNode, unsigned char* pu8Data)
{
	int s32Ret = -1;
	unsigned short u16SectionLen;
	
	PluginDvbLogcat("PluginCreatSectionNode() - entry, ppSectionNode = %p, pu8Data = %p\r\n", ppSectionNode, pu8Data);
	if(ppSectionNode && pu8Data)
	{
		u16SectionLen = SiPsiGetSectionLength(pu8Data);
		u16SectionLen += 3;
		*ppSectionNode = PluginDvbMemAlloc(u16SectionLen+sizeof(builder_section_t));
		if(*ppSectionNode)
		{
			memcpy((*ppSectionNode)->m_au8Section, pu8Data, u16SectionLen);
			(*ppSectionNode)->m_pstNext = NULL;
			s32Ret = 0;
		}
	}
	PluginDvbLogcat("PluginCreatSectionNode() - return, s32Ret = %d\r\n", s32Ret);
	return s32Ret;
}

int PluginFreeTable(builder_section_t* pstSection)
{
	int s32Ret = -1;
	builder_section_t* pstNextSection = NULL;
	
	PluginDvbLogcat("PluginFreeTable() - entry, pstSection = %p\r\n", pstSection);
	if(pstSection)
	{
		while(pstSection)
		{
			pstNextSection = pstSection->m_pstNext;
			PluginDvbMemFree(pstSection);
			pstSection = pstNextSection;
		}
		s32Ret = 0;
	}
	PluginDvbLogcat("PluginFreeTable() - return, s32Ret = %d\r\n", s32Ret);
	return s32Ret;
}

